$(document).ready(function() {
	console.log("teacher.js start");
	//studentList.append('<span>aaaa</span>');
	getStudent();
});
//onmouseover=this.style.backgroundColor='#ffff66' onmouseout=this.style.backgroundColor='#d4e3e5'
function getStudent(){
	$.ajax({
		url: '../teacher/getStudent',
		type: 'GET',
		dataType: 'json',
		data: {level: '0'},
		success: function(data){
			console.log("ajax success!!!");
			console.log(data.userList);
			var list = data.userList;
			var html = "<table id='listTable' class='hovertable'>";
			html = html + "<tr onmouseover=this.style.backgroundColor='#ffff66' onmouseout=this.style.backgroundColor='#d4e3e5'>"+
				"<td class='nametd'>*学生姓名*</td></tr>";
			for (var i = 0; i<list.length; i++) {
				html = html + "<tr onmouseover=this.style.backgroundColor='#ffff66' onmouseout=this.style.backgroundColor='#d4e3e5'>"+
				"<td class='nametd' id='"+list[i][0]+"'>" + list[i][1] + "</td></tr>";
			};
			html += "</table>";
			$("#studentList").html(html);
			$('#listTable td').each(function(index, el) {
				$(this).click(function(event) {
					getScore(this.id);
				});
			});
		},
		error: function(){
			console.log("ajax fail!!!");
			alert(errorThrown);
		}
	});
}
// <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
// 	<td>Item 5A</td><td>Item 5B</td><td>Item 5C</td>
// </tr>
function getScore(userid){
	$.ajax({
		url: '../test/getAllScore/' + userid,
		type: 'GET',
		dataType: 'json',
		data: {level: '0'},
		success: function(data){
			console.log("ajax success!!!");
			console.log(data.scoreList);
			console.log(data.percent);
			update(handleScore(data.scoreList),data.percent*100);
		},
		error: function(){
			console.log("ajax fail!!!");
			alert(errorThrown);
		}
	});
}

function handleScore(scores){
	var dataSet = {};
	var dataNum = 10;
	dataset = [];
	xMarks = [];
	var scoreLength = scores.length;
	for (var i = 0; i < dataNum; i++) {
		if(scoreLength<=10){
			if(i<scoreLength){
				dataset.push(scores[i]);
			}else{
				dataset.push(0);
			}
		}else if(scoreLength>10){
			dataset.push(scores[scoreLength - 10 + i]);
		}
		xMarks.push(i+1);
	}
	dataSet.dataset = dataset;
	dataSet.xMarks = xMarks;
	return dataSet;
}